
#ifndef READ_REMO
#define READ_REMO
//Coding for type of weather variable
//0=precip=precipitation-->filestart=1948 !!!!!!musst du noch durch 10 dividieren
//1=temp=temperature-->filestart=1948     !!!!!!musst du noch durch 100 dividieren
//2=rad= radiation-->filestart=1958
//3=precip_days=??
//#include "stdafx.h"
class read_remo
{
	vector<string> names;
	vector<FILE*> ins;
	vector<int> flag;
	vector<int> filestart;
	vector<int> fileend;
	vector<double> fact;
	string root;


	int pos_lat (double lat)
	{
		int lat_oz;
	// 180 werte mit je 0.25 degree
	// range between 30 und 75
	// Eingabe latitude als dezimal
		lat=75-lat;
		lat=floor(4*lat)/4;
		lat_oz= (int)floor((lat)/0.25+1);
		return lat_oz;
	}
	int pos_lon (double lon)
	{
		int lon_oz;
	// 300 werte mit je 0.25 degree
	// range between -15 und 60
	// Eingabe latitude als dezimal
		lon=lon+15;
		lon=floor(4*lon)/4;
		lon_oz= (int)floor((lon)/0.25+1);
		return lon_oz;
	}
	float read_weather_remo(int file, int year, int mon,double lat,double lon);
	//FILE *weather,*precip, *rad, *precip_dens, *tempstream;
public:
read_remo(string root);
~read_remo();
//void open_precip_remo (char root[90])
//{
//    char file [100]="REMO_precip1948_2005.dat";
//    char name[100];
//    strcpy(name,root);
//    strcat(name,file);
//    if((precip=fopen(name,"r"))== NULL)
//    {
//            printf( "\n");
//            printf("!!!FAIL!!! ..%s\n",name);
//    }
//    else
//    {
//            printf( "\n");
//            printf ("....reading %s\n",name);
//    }
//}
//void open_temp_remo (char root[90])
//{
//    char file [100]="REMO_temp1948_2005.dat";
//    char name[100];
//    strcpy(name,root);
//    strcat(name,file);
//    if((tempstream=fopen(name,"r"))== NULL)
//    {
//            printf( "\n");
//            printf("!!!FAIL!!! ..reading %s\n",name);
//    }
//    else
//    {
//            printf( "\n");
//            printf ("....reading %s\n",name);
//    }
//}
//void open_pd_remo (char root[90])
//{
//    char file [100]="PRECIP_days1958_2004.dat";
//    char name[100];
//    strcpy(name,root);
//    strcat(name,file);
//    if((precip_dens=fopen(name,"r"))== NULL)
//    {
//            printf( "\n");
//            printf("!!!FAIL!!! ..reading %s\n",name);
//    }
//    else
//    {
//            printf( "\n");
//            printf ("....reading %s\n",name);
//    }
//}
//void open_rad_remo (char root[90])
//{
//    char file [100]="Radiation_monthly1958_2004.dat";
//    char name[100];
//    strcpy(name,root);
//    strcat(name,file);
//    if((rad=fopen(name,"r"))== NULL)
//    {
//            printf( "\n");
//            printf("!!!FAIL!!! ..reading %s\n",name);
//    }
//    else
//    {
//            printf( "\n");
//            printf ("....reading %s\n",name);
//    }
//}
//void close_precip_remo()
//{
//	if( fclose( precip ) )
//		{
//			printf( "!!!FAIL!!! ..close source\n");
//		}
//}
//void close_rad_remo()
//{
//	if( fclose( rad ) )
//		{
//			printf( "!!!FAIL!!! ..close " );
//		}
//}
//void close_pd_remo()
//{
//	if( fclose( precip_dens ) )
//		{
//			printf( "!!!FAIL!!! ..reading " );
//		}
//}
//void close_temp_remo()
//{
//	if( fclose( tempstream ) )
//		{
//			printf( "!!!FAIL!!! ..close" );
//		}
//}

// picking just one value from the files

double calc_weather_remo(int file,int year, int mon, double lati,double loni)
{
		
	return read_weather_remo(file,year,mon,lati,loni);
}

double calc_longterm_weather_remo(int file,int startyear,int endyear, int mon, double lati,double loni)
{
	int row;
	double sum=0;
	
	for(row=startyear;row<=endyear;row++)
		{
			sum=sum+read_weather_remo(file,row,mon,lati,loni);
		}
	sum=sum/(endyear-startyear);
	return sum;
}

 double calc_seasonal_weather_mean (int file,double lati,double loni,int year,int month_start,int month_end)//1948-2004
{
	//calculiert das Mittel f�r einen Zeitraum <=12 Monate
	int mon,mon1;
	float sum=0, count=0;
	
	mon=month_start;
	do{
		mon1=mon;
		sum=sum+read_weather_remo(file,year,mon,lati,loni);
		if (mon+1==13){
			mon=1;year++;}
		else{
			mon++;}
		count++;}
	while((mon1)!=month_end);

	sum=sum/count;
	return sum;	
}
 double calc_seasonal_weather_sum (int file,double lati,double loni,int year,int month_start,int month_end)//1948-2004
{
 //calculiert die summe f�r einen Zeitraum <=12 Monate
	int mon,mon1;
	float sum=0, count=0;
	
	mon=mon1=month_start;
	
	do{
		mon1=mon;
		sum=sum+read_weather_remo( file,  year,  mon,  lati,  loni);
		if (mon+1==13){
			mon=1;year++;}
		else{
			mon++;}
		count++;}
	while((mon1)!=month_end);
	return sum;	
}

};
#endif